<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/20
 * Time: 14:27
 */
namespace App\Admin\Controllers;

use App\Admin\Traits\UserTrait;
use App\Helper;
use App\Http\Controllers\Controller;
use App\Lib\Sdk\Manager;
use App\Models\Circle;
use App\Models\Forms\JdSpiderConfig;
use App\Models\Forms\JdSpiderConfigV2;
use App\Models\Forms\PddSpiderConfig;
use App\Models\Forms\TbSpiderConfig;
use App\Models\Forms\Template;
use App\Models\Send;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;

class SpiderController extends Controller
{
    use UserTrait;

    public function taobao(Request $request, Content $content)
    {
        if ($request->getMethod() == "GET") {

            $model = new TbSpiderConfig();
            $model->action(URL::current());
            return $content
                ->title("采集配置")
                ->body($model);
        } else {
            $data = $request->post();
            $spider = Manager::getSpider(Manager::TAOBAO);
            $res = $spider->searchV2($data);
            if(!$res['code']){
                admin_toastr($spider->getErrorMessage(), "error");
                return back();
            }
            admin_toastr("采集了" . count($res['data']) . "条数据", "success");
            foreach ($res['data'] as $item) {
                if ($data['send_type'] == 0) {
                    Send::saveData(Template::getTaobaoProduct($item), $this->getUserId(),  0);
                } else {
                    Circle::saveData(Template::getTaobaoProduct($item), $this->getUserId());
                }
            }
            return back();
        }
    }


    public function jd(Request $request, Content $content)
    {
        if ($request->getMethod() == "GET") {

            $model = new JdSpiderConfigV2();
            $model->action(URL::current());
            return $content
                ->title("采集配置")
                ->body($model);
        } else {
            $data = $request->post();
            $spider = Manager::getSpider(Manager::JD);
            //list($sortName, $sortType) = explode("_", $data['sortName']);
            //$data['sortName'] = $sortName;
            //$data['sort'] = $sortType;
            $res = $spider->search($data);
            admin_toastr("采集了" . count($res) . "条数据", "success");
            foreach ($res as $item) {
                if ($data['send_type'] == 0) {
                    Send::saveData(Template::getJdProduct($item), $this->getUserId(), 0);
                } else {
                    Circle::saveData(Template::getJdProduct($item), $this->getUserId());
                }
            }
            return back();
        }
    }


    public function pdd(Request $request, Content $content)
    {
        if ($request->getMethod() == "GET") {

            $model = new PddSpiderConfig();
            $model->action(URL::current());
            return $content
                ->title("采集配置")
                ->body($model);
        } else {
            $data = $request->post();
            $spider = Manager::getSpider(Manager::PDD);
            unset($data['_token']);
            $rangeList = [];
            if ($data['min_yj'] > 0) {
                array_push($rangeList, [
                    'range_from' => $data['min_yj'],
                    'range_id' => '6',
                ]) ;
            } else {
                array_push($rangeList, [
                    'range_from' => 1,
                    'range_id' => 6,
                ]) ;
            }
            if ($data['min_sales'] > 0) {
                array_push($rangeList, [
                    'range_from' => $data['min_sales'],
                    'range_id' => 5,
                ]) ;
            }
            if ($data['max_coupon_price'] > 0) {
                array_push($rangeList, [
                    'range_from' => 0,
                    'range_id' => 1,
                    'range_to' => $data['max_coupon_price'],
                ]) ;
            }
            unset($data['min_yj'], $data['min_sales'], $data['max_coupon_price']);
            $data['range_list'] = json_encode($rangeList);
            $res = $spider->search($data);
            admin_toastr("采集了" . count($res) . "条数据", "success");
            foreach ($res as $item) {
                if ($data['send_type'] == 0) {
                    Send::saveData(Template::getPddProduct($item), $this->getUserId(), 0);
                } else {
                    Circle::saveData(Template::getPddProduct($item), $this->getUserId());
                }
            }

            return back();
        }
    }

}